/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package tvuong.gsb;

import modele.DAO;
import modele.Med;
import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author tvuong
 */
public class ListeMed extends ListActivity {

    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.listemed);

        TextView label = (TextView) findViewById(R.id.label);
        Intent inter = getIntent();
        String num = inter.getStringExtra("leNum");
        label.setText(num);

        final List<Med> lesMeds = DAO.getLesMedsParDep(num);
        MedAdapter adapter = new MedAdapter(lesMeds, this);
        setListAdapter(adapter);

        final EditText keyInput = (EditText) findViewById(R.id.textSearch);
        Button button = (Button) findViewById(R.id.buttonSearch);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                List<Med> lesMedsSearch = new ArrayList<Med>();
                for (Med unMed : lesMeds) {
                    if (unMed.getNom().startsWith(keyInput.getText().toString())) {
                        lesMedsSearch.add(unMed);
                    }
                }
                MedAdapter adapter = new MedAdapter(lesMedsSearch, getApplicationContext());
                setListAdapter(adapter);
            }
        });
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        Intent CallIntent = new Intent(Intent.ACTION_CALL);
        CallIntent.setData(Uri.parse("tel:"+((Med) l.getItemAtPosition(position)).getTel()));
        startActivity(CallIntent);
    }
}
